package com.monna.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * 设置数字方块
 * @author 廖梦青
 */
public class GamesItem extends View {

    /**
     * 小方块数字
     */
    private int number = 0;
    private String numberVal;
    /**
     * 画笔
     */
    private Paint paint = new Paint();
    /**
     * rect：方块（正方形或长方形），只有四个属性left top right bottom，分别指方块四边到屏幕四边的距离
     */
    private Rect bound;


    public GamesItem(Context context) {
        this(context, null);
    }

    public GamesItem(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GamesItem(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 获取数字
     * @return
     */
    public int getNumber(){
        return number;
    }

    /**
     * 绘制数字
     * @param num
     */
    public void setNumber(int num){
        number = num;
        numberVal = number + "";
        float size = 50.0f;
        paint.setTextSize(size);
        bound = new Rect();
        //获取数字文本的边界
        paint.getTextBounds(numberVal, 0, numberVal.length(), bound);

        //重新绘制
        invalidate();
    }

    /**
     * 绘制数字方块，默认所有Item的数字都是0
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        String bgColor;
        switch (number){
            case 0:
                bgColor = "#c9bcb3";
                break;
            case 2:
                bgColor = "#eee4da";
                break;
            case 4:
                bgColor = "#ece0c8";
                break;
            case 8:
                bgColor = "#f2b179";
                break;
            case 16:
                bgColor = "#f59563";
                break;
            case 32:
                bgColor = "#f57c5f";
                break;
            case 64:
                bgColor = "#f65d3b";
                break;
            case 128:
                bgColor = "#edce71";
                break;
            case 256:
                bgColor = "#edcc61";
                break;
            case 512:
                bgColor = "#ecc850";
                break;
            case 1024:
                bgColor = "#edc53f";
                break;
            case 2048:
                bgColor = "#eec22e";
                break;
            case 4096:{
                bgColor = "#1b1b1b";
                break;
            }
            default:
                bgColor = "#c9bcb3";
        }
        paint.setColor(Color.parseColor(bgColor));
        paint.setStyle(Paint.Style.FILL);
        canvas.drawRect(4, 18, getWidth()+3, getHeight()+3, paint);
        if (number != 0){
            paint.setColor(Color.parseColor("#696969"));
            paint.setTextSize(84f);
            float x = (getWidth() - bound.width()) / 2;
            float y = (getHeight() + bound.height()) / 2 + 18;
            canvas.drawText(numberVal, x, y, paint);
        }
    }
}
